import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
import Base from "../components/Base";
import Login from "../components/Login";
import ShowCenter from "../components/ShowCenter";
import Host from '@/components/Host'

const router = new Router({
  mode: "history",
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login,
    },
    {
      path: '/hippo',
      name: 'Base',
      component: Base,
      children: [
        {
          path: 'workbench',  // 访问路径： 父级路由 + 当前路由
          name: 'ShowCenter',
          component: ShowCenter,
        },
        {
          path: 'host',
          name: 'Host',
          component: Host,
        },
      ]
    },

  ]
})

// 路由守卫[导航守卫]
import axios from "axios"
import settings from "../settings.js"
router.beforeEach((to, from, next) => {
  if(to.name === 'Login'){
    next()
    return
  }
  let token = sessionStorage.token || localStorage.token;
  // 验证jwt token
  axios.post(`${settings.host}/users/refresh`,{
    token, // token: token,
  }).then(response=>{
      // 保存返回的新token
      sessionStorage.token = response.data.token;
      next()
  }).catch(error=>{
      // alert("对不起，您尚未登录或登录已超时，请重新登录！")
      next("/")
  })

})


export default router
